﻿using Demo.Net.API.Model;
using Demo.Net.RabbitMQ;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Bson.IO;
using RabbitMQ.Client;
using Newtonsoft.Json;
using NetTaste;
using JsonConvert = Newtonsoft.Json.JsonConvert;

namespace Demo.Net.API.Controllers
{


    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RabbitMQController: ControllerBase
    {

        private readonly IRabbitMQClient _rabbitMQClient;


        public RabbitMQController(IRabbitMQFactory rabbitMQFactory) 
        {
            _rabbitMQClient = rabbitMQFactory.Get();
        }


        [HttpGet]
        public async Task<IActionResult> PublicDirectMQTest()
        {

            UserInfo user = new UserInfo();
            user.UserId = 1;
           user.Age = 30;
            user.Name = "Test";
      
            //Direct
            _rabbitMQClient.DirectPubilsh("Test","TestExchange", JsonConvert.SerializeObject(user));



            return Ok();
        }

        [HttpGet]
        public async Task<IActionResult> PublicFanoutMQTest()
        {

            UserInfo user = new UserInfo();
            user.UserId = 1;
            user.Age = 30;
            user.Name = "Test";
            _rabbitMQClient.FanoutPublish(new List<string> { "FanoutQuque01", "FanoutQuque02", "FanoutQuque03" }, "FanoutExchange", JsonConvert.SerializeObject(user));

            return Ok();
        }


        [HttpGet]
        public async Task<IActionResult> PublicHeaderMQTest()
        {

            UserInfo user = new UserInfo();
            user.UserId = 1;
            user.Age = 30;
            user.Name = "Test";
            _rabbitMQClient.HeaderPublish("HeaderExchange", JsonConvert.SerializeObject(user),new Dictionary<string, object> {
                { "key1",1},
                { "key2",true},
                {"key3","test" }
            });

            return Ok();
        }


        [HttpGet]
        public async Task<IActionResult> PublicTopicMQTest()
        {
            _rabbitMQClient.TopicPublish("TopicExchange", "第一条消息","demo.net");


            _rabbitMQClient.TopicPublish("TopicExchange", "第二条消息", "demo.mq");


            _rabbitMQClient.TopicPublish("TopicExchange", "第三条消息", "mq.net");

            return Ok();
        }
    }
}
